package com.lmh.utils;

import com.github.pagehelper.PageInfo;
import com.lmh.entity.User;

import java.util.Map;

public class Result {

    public static Integer SUCCESS_CODE = 200;
    public static Integer ERROR_CODE = 500;

    public Integer status = SUCCESS_CODE;
    public String msg = "操作成功";
    public Object data = null;

    // 成功
    public static Result ok(Object data) {
        return new Result(data);
    }

    public static Result ok(String msg, Object data) {
        return new Result(msg, data);
    }

    public static Result ok(Integer status, String msg, Object data) {
        return new Result(status, msg, data);
    }

    public static Result ok() {
        return new Result();
    }

    public static <T> Map<String, Object> ok(PageInfo<T> pageInfo) {
        return Maps.build()
                .add("status", 200)
                .add("msg", "操作成功")
                .add("total", pageInfo.getTotal())
                .add("list", pageInfo.getList())
                .getParamMap();
    }

    // 失败
    public static Result fail() {
        return new Result(ERROR_CODE, "操作失败");
    }

    public static Result fail(String msg) {
        return new Result(ERROR_CODE, msg, null);
    }

    public static Result fail(Integer status, String msg) {
        return new Result(status, msg, null);
    }

    // 构造函数
    public Result() {

    }

    public Result(Object data) {
        this.data = data;
    }

    public Result(String msg, Object data) {
        this.msg = msg;
        this.data = data;
    }

    public Result(Integer status, String msg, Object data) {
        this.status = status;
        this.msg = msg;
        this.data = data;
    }

    public Result(Integer status, String msg) {
        this.status = status;
        this.msg = msg;
    }

    public Integer getStatus() {
        return status;
    }

    @Override
    public String toString() {
        return "Result{" +
                "status=" + status +
                ", msg='" + msg + '\'' +
                ", data=" + data +
                '}';
    }
}
